/**
 * Title:
 * Description:
 * @author jjtEatJava
 * @date 2017年7月28日
 */
package com.kiki.jjt.freemark;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

/**
 * Title:
 * Description:
 * @author jjtEatJava
 * @date 2017年7月28日
 */
public class Main {
	private Configuration cfg  = null;
	public Main() throws IOException {
		// Create your Configuration instance, and specify if up to what FreeMarker
		// version (here 2.3.22) do you want to apply the fixes that are not 100%
		// backward-compatible. See the Configuration JavaDoc for details.
		cfg = new Configuration(Configuration.VERSION_2_3_23);

		// Specify the source where the template files come from. Here I set a
		// plain directory for it, but non-file-system sources are possible too:
		cfg.setDirectoryForTemplateLoading(new File(ClassLoader.getSystemResource("").getPath(), "templates"));

		// Set the preferred charset template files are stored in. UTF-8 is
		// a good choice in most applications:
		cfg.setDefaultEncoding("UTF-8");

		// Sets how errors will appear.
		// During web page *development* TemplateExceptionHandler.HTML_DEBUG_HANDLER is better.
		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

	}
	
	public Configuration getCfg() throws IOException {
		return cfg;
	}
	
	public static void main(String[] args) throws IOException, TemplateException {
		String packageName = "team.bingostar.qa";
		String path = "H:\\test";
		
		Configuration cfg = new Main().getCfg();
		cfg.setSharedVariable("packageName", packageName);
		
		Template baseServiceT = cfg.getTemplate("BaseService.ftl");
		Template baseServiceImplT = cfg.getTemplate("BaseServiceImpl.ftl");
		Template entityServiceT = cfg.getTemplate("EntityService.ftl");
		Template entityServiceImplT = cfg.getTemplate("EntityServiceImpl.ftl");
		
		//创建文件夹
		File file = new File(path);
		if(!file.exists())
			file.mkdir();
		
		//创建BaseService
		String baseServiceFileName = "BaseService.java";
		File  baseServiceFile= new File(path,baseServiceFileName);
		if(!baseServiceFile.exists())
			baseServiceFile.createNewFile();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("packageName", packageName);
		Writer writer = new StringWriter();
		baseServiceT.process(map,writer);
		String baseServiceCode = writer.toString();
		RandomAccessFile out= new RandomAccessFile(baseServiceFile,"rw");
		FileChannel baseServiceCh =out.getChannel();
		MappedByteBuffer buff = baseServiceCh.map(FileChannel.MapMode.READ_WRITE, 0, baseServiceCode.length());
		long s =System.currentTimeMillis();
		byte[] bytes=  baseServiceCode.getBytes();
		for(int i=0;i<bytes.length;i++) {
			buff.put(bytes[i]);
		}
		long e = System.currentTimeMillis();
		System.out.println(e-s);
		baseServiceCh.close();
		out.close();
	}
}
